from Bases import BaseObject


class BaseIO(BaseObject):
    def __init__(self, **kwargs):
        super(BaseIO, self).__init__(**kwargs)
        self.register_signal('state_changed')
        self.register_signal('data_received')
        self._connected = False
        
        self.client = None
        
    def _send(self, data):
        pass
            
    def on_data_received(self, *args, **kwargs):
        data = kwargs.get('data')
        if data is None:
            data = args[0]
        self.emit('data_received', data=data, client=self)
        
    def on_client_disconnect(self, connector, reason):
        self.client = None
        self.connected = False
        #self.emit('state_changed', state=False)
        
    def do_connect(self, *args, **kwargs):
        pass
        
    def do_disconnect(self):
        pass
        
    @property
    def connected(self):
        return self._connected
    @connected.setter
    def connected(self, value):
        if value != self._connected:
            self._connected = value
            self.emit('state_changed', state=value)
